<?php
/**
 * Created by IntelliJ IDEA.
 * User: Winnie
 * Date: 07/04/14
 * Time: 13:49
 */

    include("gen.php");
    $cmd=get_datan("cmd");
    switch($cmd){
        case 1:
            //get one user based n id
           get_user();
            break;
        case 2:
            //get all user records and return as array
            get_all_users();
            break;
        case 3:
            update_user();
            break;
        case 4:
            add_user();
            break;
        case 5:
            search_by_username();
            break;
        case 6:
            deleteItem();
            break;
        default:
            echo "{";
            echo jsonn("result",0).",";
            echo jsons ("message", "unknown command");
            echo "}";
    }


    function get_user(){
        include("users.php");
        $user = new users();


        $id=get_datan("id");
        $row=$user->search_byId($id);


      //  $user->fetch();
        if(!$row){
            echo "{";
            echo jsonn ("result",0).",";
            echo jsons ("message","user not found");
            echo "}";
            return;
        }

        echo "{";
            echo jsonn ("result",1).",";
            echo '"user":{';
                echo jsonn ("id",$id).",";
                echo jsons ("firstname",$row['firstname']).",";
                echo jsons ("lastname",$row['lastname']).",";
                echo jsons ("username",$row['username']).",";
                echo jsons ("usertype",$row['usertype']);
            echo "}";
        echo "}";
    }

    function get_all_users(){
        include_once("users.php");

        $user = new users();
        $user -> get_all_users();
        $row=$user->fetch();

        if(!$row){
            echo "{";
                echo jsonn ("result",0);
                echo jsons ("message","error retrieving users");
            echo "}";
            return;
        }
            while ($row){
                echo "{";
                    echo jsonn ("result",1).",";
                    echo '"user":{[';
                        echo jsonn ("id",$row['userId']).",";
                        echo jsons ("firstname",$row['firstname']).",";
                        echo jsons ("lastname",$row['lastname']).",";
                        echo jsons ("username",$row['username']).",";
                        echo jsons ("usertype",$row['usertype']);
                    echo "]}";
                echo "}";
                $row = $user->fetch();
            }
    }

    function update_user(){
        include_once("users.php");

        $user = new users();

        $id=get_datan("id");
        $first_name=get_data("fName");
        $last_name = get_data("lName");
        $username = get_data("uName");
        $user_type = get_data("uType");

        if(!$user -> edit_user($first_name,$last_name,$username,$user_type,$id)){
            echo "{";
                echo jsonn ("result",0).",";
                echo jsons ("message","error updating user");
            echo "}";
            return;
        }
         echo "{";
            echo jsonn ("result",1).",";
            echo jsons ("message","record updated");
         echo "}";

    }

    function add_user(){
        include_once("users.php");
        $user = new users();

        $id=get_datan("id");
        $first_name=get_data("fName");
        $last_name = get_data("lName");
        $username = get_data("uName");
        $pass_word = get_data("pWord");
        $user_type = get_data("uType");

        $date_created = date("Y/m/d"); //or Y/m/d

        if(!$user->add_user($id,$first_name,$last_name,$username,$user_type,$pass_word,$date_created)){
            echo "{";
                echo jsonn ("result",0).",";
                echo jsons ("message","error adding user");
            echo "}";
            return;
        }
            echo "{";
                echo jsonn ("result",1).",";
                echo jsons ("message","user added");
            echo "}";
 }

    function search_by_username(){
        include_once("users.php");
        $user = new users();

        $user_name=get_data("u_name");

        $user -> search_user_by_username($user_name);

        $row = $user -> fetch();

        if(!$row){
            echo "{";
                echo jsonn ("result",0).",";
                echo jsons ("message","error searching");
            echo "}";
            return;
        }
           echo "{";
                echo jsonn ("result",1).",";
                echo '"user":[';
                $each =0;
                while($row){
                    if($each > 0){
                        echo ",{";
                    }
                    else{
                        echo "{";
                    }
                    echo jsonn ("id",$row['userId']).",";
                    echo jsons ("firstname",$row['firstname']).",";
                    echo jsons ("lastname",$row['lastname']).",";
                    echo jsons ("username",$row['username']).",";
                    echo jsons ("usertype",$row['usertype']);
                    echo "}";
                    $row = $user -> fetch();
                    $each ++;
                }
                 echo "]";
           echo "}";

        }

        function delete(){
            include_once("users.php");
            $user = new users();

            $uId = get_datan("userId");
            if(!$user->delete($uId)){
                echo "{";
                    echo jsonn ("result",0).",";
                    echo jsons ("message","delete wasn't successful");
                echo "}";
                return;
            }
                echo "{";
                    echo jsonn ("result",1).",";
                    echo jsons ("message","delete was successful");
                echo "}";

        }